package com.itboye.lottery.fragment;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.ObservableScrollView;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.itboye.bylibrary.widget.glide.GlideUtil;
import com.itboye.lottery.base.BaseFragment;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.LoginActivity;
import com.itboye.lottery.activity.MyAttentionActivity;
import com.itboye.lottery.activity.WebActivity;
import com.itboye.lottery.activity.user.AdviceActivity;
import com.itboye.lottery.activity.user.ChongZhi_TiXianMingXi_ZhangHuMingXi_ListActivity;
import com.itboye.lottery.activity.user.ChongzhiActivity;
import com.itboye.lottery.activity.user.ContactKeFuActivity;
import com.itboye.lottery.activity.user.MessageCenterActivity;
import com.itboye.lottery.activity.user.SetActivity;
import com.itboye.lottery.activity.user.TiXianActivity;
import com.itboye.lottery.activity.user.TouZhuRecordActivity;
import com.itboye.lottery.activity.user.UserDataActivity;
import com.itboye.lottery.activity.user.ZhuihaoManageActivity;
import com.itboye.lottery.adapter.MyLotteryBottomAdapter;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.bean.UserDataBean;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.utils.Const;
import com.itboye.lottery.utils.SPUtils;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;

import static com.itboye.lottery.utils.ConstUrl.TYPE_ABOUT;
import static com.itboye.lottery.utils.ConstUrl.about_url;
import static com.itboye.lottery.utils.SPUtils.getSp;

/**
 * Created by Administrator on 2017/11/10.
 */

public class MyLotteryFragment extends BaseFragment implements ResponseCallback {

    TextView textView, user_nick;
    ImageView iv_set;
    RecyclerView fragment_rv_my_lottery;
    ImageView user_head;
    private MyLotteryBottomAdapter adapter;
    private ArrayList<HashMap<String, Object>> hashMapArrayList;
    UserPresenter userPresenter;
    private String amount;
    TextView tv_available_present_balance, tv_available_balance;
    LinearLayout ll_balance;
    TextView tv_user_attention, tv_user_fans;
    PullToRefreshScrollView pull_to_refresh_scrollView;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_lottery;
    }

    @Override
    protected void initData() {
        userPresenter = new UserPresenter(this);
        pull_to_refresh_scrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ObservableScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {
                setUserData();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ObservableScrollView> refreshView) {

            }
        });
        initRecData();
    }


    private void initRecData() {
        fragment_rv_my_lottery.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        hashMapArrayList = getRecyData();
        adapter = new MyLotteryBottomAdapter(hashMapArrayList);
        fragment_rv_my_lottery.setAdapter(adapter);
        adapter.setOnItemClickListener((view, position) -> {
            switch (position) {
                case 0:
                    //充值
                    startActivity(new Intent(getActivity(), ChongzhiActivity.class));
                    break;
                case 1:
                    //提现
                    startActivity(new Intent(getActivity(), TiXianActivity.class));
                    break;
                case 3:
                    //投注记录
                    startActivity(new Intent(getActivity(), TouZhuRecordActivity.class).putExtra("type", 0));
                    break;
                case 2:
                    //账户明细
                    startActivity(new Intent(getActivity(), ChongZhi_TiXianMingXi_ZhangHuMingXi_ListActivity.class).putExtra("type", 1));
                    break;
                case 4:
                    //消息中心
                    startActivity(new Intent(getActivity(), MessageCenterActivity.class));
//                    startActivity(new Intent(getActivity(), WebActivity.class).putExtra("title", hashMapArrayList.get(position).get("title") + "").putExtra("url", hashMapArrayList.get(position).get("url") + ""));
                    break;
                case 5:
                    //追号管理
                    startActivity(new Intent(getActivity(), ZhuihaoManageActivity.class));
                    break;
                case 6:
                    //联系客服
                    startActivity(new Intent(getActivity(), ContactKeFuActivity.class));
                    break;
                case 7:
                    startActivity(new Intent(getActivity(), AdviceActivity.class));
                    break;
                case 8:
                    startActivity(new Intent(getActivity(), WebActivity.class).putExtra("title", "关于我们").putExtra("url", about_url + "?type=" + TYPE_ABOUT));
                    break;
                case 9://跟单记录
                    startActivity(new Intent(getActivity(), TouZhuRecordActivity.class)
                            .putExtra("type", 1));
                    break;
                case 10://我的邀请码查看
                    if (TextUtils.isEmpty(getSp(Const.Companion.getInvitation_code()) + "")) {
                        ByAlert.alert("暂无可用邀请码");
                    } else {
                        ByAlert.alert(String.format("您的邀请码是：%s", getSp(Const.Companion.getInvitation_code())));
                    }
                    break;
            }

        });
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.HORIZONTAL);
//        dividerItemDecoration.setDrawable(new ColorDrawable(getResources().getColor(R.color.efefef)));
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.divider_item));
        fragment_rv_my_lottery.addItemDecoration(dividerItemDecoration);
        dividerItemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.divider_item));
        fragment_rv_my_lottery.addItemDecoration(dividerItemDecoration);
    }

    private ArrayList<HashMap<String, Object>> getRecyData() {
        ArrayList<HashMap<String, Object>> hashMapArrayList = new ArrayList<>();

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", "充值");
        hashMap.put("icon", R.mipmap.chongzhi);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);
        hashMap = new HashMap<>();
        hashMap.put("name", "提现");
        hashMap.put("icon", R.mipmap.tixian);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put("name", "账户明细");
        hashMap.put("icon", R.mipmap.zhanghumingxi);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put("name", "投注记录");
        hashMap.put("icon", R.mipmap.touzhujilu);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put("name", "消息中心");
        hashMap.put("icon", R.mipmap.xiaoxizhongxin);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);


        hashMap = new HashMap<>();
        hashMap.put("name", "追号管理");
        hashMap.put("icon", R.mipmap.zhuihaoguanli);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);


        hashMap = new HashMap<>();
        hashMap.put("name", "联系客服");
        hashMap.put("icon", R.mipmap.kefu);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);


        hashMap = new HashMap<>();
        hashMap.put("name", "意见反馈");
        hashMap.put("icon", R.mipmap.yijianfankui);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);


        hashMap = new HashMap<>();
        hashMap.put("name", "关于我们");
        hashMap.put("icon", R.mipmap.guanyuwomen_1);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put("name", "跟单记录");
        hashMap.put("icon", R.mipmap.gendanjilu);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);

        hashMap = new HashMap<>();
        hashMap.put("name", "我的邀请码");
        hashMap.put("icon", R.mipmap.ic_invite_code);
        hashMap.put("url", "http://www.baidu.com");
        hashMapArrayList.add(hashMap);
        return hashMapArrayList;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.user_head:
                //未登录跳转登录
                if (TextUtils.isEmpty(getSp(Const.Companion.getUID()) + "")) {
                    startActivity(new Intent(getActivity(), LoginActivity.class));
                } else {
                    startActivity(new Intent(getActivity(), UserDataActivity.class));
                }
                break;
            case R.id.iv_set:
                //跳转设置界面
                startActivity(new Intent(getActivity(), SetActivity.class));
                break;
            case R.id.tv_user_attention:
                //我的关注
                startActivity(new Intent(getActivity(), MyAttentionActivity.class));
                break;
            case R.id.tv_user_fans:
                //我的粉丝
//                startActivity(new Intent(getActivity(), MyAttentionActivity.class).putExtra("follow", 1));
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        if (isVisibleToUser) {
            setUserData();
        }
        super.setUserVisibleHint(isVisibleToUser);
    }

    @SuppressLint("SetTextI18n")
    private void setUserData() {
        String headUrl = getSp(Const.Companion.getUSER_HEAD()) + "";
        GlideUtil.displayForCircle(getActivity(), headUrl, R.mipmap.head_default, user_head);
        user_nick.setText(getSp(Const.Companion.getNICKNAME()) + "");
        if (!TextUtils.isEmpty(getSp(Const.Companion.getUID()) + "")) {
            //获取用户可提现余额
            userPresenter.getMyLotteryFragmentData();//获取可用余额、可提现余额、粉丝关注、邀请码。。。
            ll_balance.setVisibility(View.VISIBLE);
        } else {
            ll_balance.setVisibility(View.GONE);
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        if (requestSuccessType == userPresenter.getMyLotteryFragmentData_SUCCESS) {
            UserDataBean balanceBean = (UserDataBean) o;
            amount = balanceBean.getTixian();
            tv_available_present_balance.setText(Html.fromHtml("可提现余额(元)<br />" + amount));
            tv_available_balance.setText(Html.fromHtml("可用余额(元)<br />" + balanceBean.getKeyong()));
            tv_user_attention.setText("关注:" + balanceBean.getGuanzhu());
            tv_user_fans.setText("粉丝:" + balanceBean.getFensi());
            SPUtils.put(getActivity(), null, Const.Companion.getInvitation_code(), balanceBean.getInvitation_code());
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
        pull_to_refresh_scrollView.onRefreshComplete();
        ByAlert.alert(msg);
    }
}
